//
//  ViewController.m
//  CircleRotateMenu
//
//  Created by JLei on 2018/6/28.
//  Copyright © 2018年 Broccolis. All rights reserved.
//

#import "ViewController.h"
#import "BroccolisCircleMenu.h"

//屏幕宽
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
//屏幕高
#define kScreenHeight [UIScreen mainScreen].bounds.size.height


@interface ViewController ()<Bro_CircleViewDelegate>

@property (nonatomic,strong) UIButton *circleCenterBtn;
@property (nonatomic,strong) BroccolisCircleMenu *circleMenu;
@property (nonatomic,strong) UIPanGestureRecognizer *pan;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blackColor];
    [self addCircleMenu];
    
}

- (void)addCircleMenu {
    
    //触发按钮
    self.circleCenterBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 80, kScreenHeight - 250, 80, 80)];
    [self.circleCenterBtn setImage:[UIImage imageNamed:@"greencenter"] forState:0];
    self.circleCenterBtn.layer.masksToBounds = YES;
    self.circleCenterBtn.layer.cornerRadius = 40;
    [self.circleCenterBtn addTarget:self action:@selector(showItems:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.circleCenterBtn];
    self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.circleCenterBtn addGestureRecognizer:self.pan];
    NSArray *titlesArray = @[@"行程",@"账单",@"乘车码",@"预付票",@"已购票",@"消息中心",@"设置",@"乘车码"];
    NSArray *imagesArray = @[@"行程绿色",@"帐单G",@"乘车码G",@"预付票G",@"已购票G",@"消息中心G",@"个人信息G",@"乘车码G"];
    
    self.circleMenu = [BroccolisCircleMenu sharedCircleView];
    [self.circleMenu configBtnWithTitles:titlesArray andImages:imagesArray];
    self.circleMenu.delegate = self;
    self.circleMenu.center = CGPointMake(self.circleCenterBtn.center.x+1, self.circleCenterBtn.center.y);
    [self.view addSubview:self.circleMenu];
    
    
}


#pragma mark 拖动手势
- (void)panAction:(UIGestureRecognizer *)gesture {
    
    CGPoint point = [gesture locationInView:self.view];
    
    self.circleCenterBtn.center = point;
    
    if (gesture.state == UIGestureRecognizerStateChanged) {}
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        
        CGRect rect = self.circleCenterBtn.frame;
        
        if (self.circleCenterBtn.center.x > kScreenWidth * 0.5) {
            rect.origin.x = kScreenWidth - self.circleCenterBtn.frame.size.width;
        }
        else {
            rect.origin.x = 0;
        }
        
        if (self.circleCenterBtn.frame.origin.y < 140) {
            rect.origin.y = 140;
        }
        
        if (self.circleCenterBtn.frame.origin.y > kScreenHeight - 200) {
            rect.origin.y = kScreenHeight - 200;
        }
        
        self.circleCenterBtn.frame = rect;
        self.circleMenu.center = CGPointMake(self.circleCenterBtn.center.x, self.circleCenterBtn.center.y);
        
        if (rect.origin.x > kScreenWidth * 0.5) {
            //右侧
            
        } else {
            //左侧
            
        }
        
    }
    
}

#pragma mark 展开圆盘
- (void)showItems:(UIButton *)sender{
    //展示与隐藏
    [self.circleMenu show];
}

#pragma mark 点击周边按钮
- (void)clickBtnWithIndex:(NSInteger)index {
    NSLog(@"点击了第%zd个按钮",index);
}

- (void)showStaticBtn:(BOOL)isShow {
    NSLog(@"%@",isShow == YES ? @"展开" : @"收起");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
